package deepr.lib;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NameLib {


  public static String captureName(String name) {
    char[] cs = name.toCharArray();
    cs[0] -= 32;
    return String.valueOf(cs);
  }

  public static String firstLowerName(String name) {
    char[] cs = name.toCharArray();
    if (Character.isLowerCase(cs[0]))
      return name;
    cs[0] += 32;
    return String.valueOf(cs);
  }

  private static Pattern linePattern = Pattern.compile("_(\\w)");

  /**
   * 下划线转驼峰
   */
  public static String lineToHump(String str) {
    str = str.toLowerCase();
    Matcher matcher = linePattern.matcher(str);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
      matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
    }
    matcher.appendTail(sb);
    return sb.toString();
  }

  private static Pattern humpPattern = Pattern.compile("[A-Z]");

  /**
   * 驼峰转下划线,效率比上面高
   */
  public static String humpToLine(String str) {
    Matcher matcher = humpPattern.matcher(str);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
      matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
    }
    matcher.appendTail(sb);
    return sb.toString();
  }

  public static boolean isAcronym(String word) {
    for (int i = 0; i < word.length(); i++) {
      char c = word.charAt(i);
      if (Character.isLowerCase(c)) {
        return false;
      }
    }
    return true;
  }

  public static boolean firstAcronym(String word) {
    if (!Character.isUpperCase(word.charAt(0)))
      return false;
    return isLower(word.substring(1));
  }

  public static boolean isLower(String word) {
    for (int i = 0; i < word.length(); i++) {
      char c = word.charAt(i);
      if (Character.isUpperCase(c)) {
        return false;
      }
    }
    return true;
  }

}
